package org.hu.flexeasy.ui
{
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.core.UIComponent;
	
	import org.hu.flexeasy.classes.ILoading;
	import org.hu.flexeasy.components.LoadMasks;
	import org.hu.flexeasy.components.Marks;
	
	import spark.components.BorderContainer;
	
	/**
	 * <p>
	 *     <b>HUBO 2012-1-21  /  hubo.0508ⓐgmail.com</br>org.hu.flexeasy.ui.Image.as</b>
	 * </p>
	 */
	public class Image extends mx.controls.Image implements ILoading
	{
		
		//private var loadMasks:LoadMasks;
		private var masks:Marks;
		
		////////////////////////////implements start////////////////////////////
		private var loadIdentification:Boolean = false;
		private var _loadClasses:Class;
		private var _bgColor:uint;
		private var _bgAlpha:Number;
		////////////////////////////implements end////////////////////////////
		
		private var text:Label;		
		
		public function Image()
		{
			super();

			this.addEventListener(ProgressEvent.PROGRESS,progressHanlder,false,0,true);
			this.addEventListener(Event.COMPLETE,completHandler,false,0,true);
//			this.addEventListener(Event.OPEN,openNetworkHandler,false,0,true);
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if(loadIdentification && !masks)
			{
				masks = new Marks();
			
				if(!text)
				{
					text = this.createText();
					
					masks.addElement(text);
				}
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if(masks)
			{
				masks.x = 0;
				masks.y = 0;
				
				masks.includeInLayout = false;		
				
				//set Property
				
				if(this.bgColor != 0 )
				{
					this.masks.setStyle("backgroundColor",this.bgColor);
				}
				
				if(this.bgAlpha.toString() != "NaN")
				{
					this.masks.setStyle("backgroundAlpha",this.bgAlpha);
				}
			}		
		}

		
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			super.updateDisplayList(w,h);
			
			if(masks)
			{
				masks.width = w;
				masks.height = h;
			}	
			this.removeChildAt(0);
		}
		
		protected function createText():Label
		{
			var label:Label = new Label();
			label.verticalCenter = 0;
			label.horizontalCenter = 0;
			
			return label;
		}
	
		/**
		 * 完成加载
		 */
		protected function progressHanlder(e:ProgressEvent):void
		{
//			this.addChildAt(this.masks,0);
//			this.text.text = Math.round((e.bytesLoaded * 100 / e.bytesTotal))+"%";
		}
		
		/**
		 * 加载内容时分派
		 */
		protected function completHandler(event:Event):void
		{
			if(masks)
			{
				//this.removeChild(this.masks);
			}
		}
		
//		/**
//		 * 网络操作开始时分派
//		 */
//		protected function openNetworkHandler(event:Event):void
//		{
//			if(masks)
//			{
//				this.addChildAt(this.masks,0);
//			}
//		}

		public function get loadClasses():Class
		{
			return this._loadClasses;
		}
		
		public function set loadClasses(value:Class):void
		{
			this._loadClasses = value;	
		}
		
		public function get bgColor():uint
		{
			return this._bgColor
		}
		
		public function set bgColor(value:uint):void
		{
			this._bgColor = value;
		}
		
		public function get bgAlpha():Number
		{
			return this._bgAlpha;
		}
		
		public function set bgAlpha(value:Number):void
		{
			this._bgAlpha = value;
		}
		
		/**
		 * 创建容器时，是否创建加载等待提示内容
		 */
		public function get loads():Boolean
		{
			return loadIdentification;
		}
		
		/**
		 * @private
		 */
		public function set loads(value:Boolean):void
		{
			loadIdentification = value;
		}		
	}
}